#include <iostream>
#include <ctime>
#include <unistd.h>  // fro getpid
#include <stdio.h>  // for sprintf
#include <sys/time.h>  // for gettimeofday

#include "log.h"

using namespace std;

int Log::pid = getpid();

string Log::getNowStr() {
    char buf[20], buf2[30];
    struct timeval tv;
    time_t curtime;

    gettimeofday(&tv, NULL);
    curtime = tv.tv_sec;

    strftime(buf, 30, "%Y:%m:%d %H:%M:%S", localtime(&curtime));
    sprintf(buf2, "%s.%ld", buf, tv.tv_usec);

    return string(buf2);
}

void Log::info(const string &msg) {
    cout << getNowStr() << " [" << pid << "]" << " [INFO] "
         << msg << endl;
}

void Log::warn(const string &msg) {
    cout << getNowStr() << " [" << pid << "]" << " [WARN] "
         << msg << endl;
}

void Log::error(const string &msg) {
    cerr << getNowStr() << " [" << pid << "]" << " [ERROR] "
         << msg << endl;
    cout << getNowStr() << " [" << pid << "]" << " [ERROR] "
         << msg << endl;
}
